package com.k2data.etl.datasender;

import vcommon.utils.TimeUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.sql.Time;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class S8kDataBuilder {

    private static String S8kDataTempJson = "{\"fieldGroupId\":\"{tableName}\",\"sampleTime\":{\"timestamp\":\"{timestamp}\"},\"fields\":[{\"fieldValue\":\"{factory_id}\",\"fieldId\":\"factory_id\",\"isIdField\":true},{\"fieldValue\":\"1\",\"fieldId\":\"datatype\",\"isIdField\":true},{\"fieldValue\":\"{equipment_id}\",\"fieldId\":\"equipment_id\",\"isIdField\":true},{\"fieldValue\":\"{channel_id}\",\"fieldId\":\"channel_id\",\"isIdField\":true},{\"fieldValue\":\"0\",\"fieldId\":\"channel_type\",\"isIdField\":true},{\"fieldValue\":\"{machine_id}\",\"fieldId\":\"machine_id\",\"isIdField\":true},{\"fieldValue\":28.5002956,\"fieldId\":\"vbmax\"},{\"fieldValue\":11697,\"fieldId\":\"speed\"},{\"fieldValue\":25.9683762,\"fieldId\":\"direc\"},{\"fieldValue\":7.668022,\"fieldId\":\"rms\"},{\"fieldValue\":1,\"fieldId\":\"type\"},{\"fieldValue\":7.024507,\"fieldId\":\"rvn1x\"},{\"fieldValue\":1,\"fieldId\":\"key_id\"},{\"fieldValue\":\"-10.3446,-9.1268,-8.0682,-11.0885,-12.9146,-11.5131,-9.5025,-4.8042,-3.6751,-4.2223,-2.3787,-0.2011,0.6678,2.0192,3.671,6.3835,5.9635,7.9044,9.0135,8.5896,6.1077,4.8125,10.731,12.0682,9.5298,6.393,4.5896,0.9898,-1.7505,-4.4259,-8.9649,-11.0161,-10.1967,-9.0549,-8.0009,-10.8689,-12.5486,-11.1561,-9.2087,-4.4527,-3.1942,-3.7424,-2.0607,-0.0196,0.8319,2.1333,3.5598,5.9814,5.3975,7.2739,8.2301,7.5411,4.8759,3.5834,9.5438,10.8117,8.189,5.1357,3.5471,0.0894,-2.628,-5.224,-9.5307,-11.3503,-10.4602,-9.3197,-8.1516,-10.8138,-12.39,-11.036,-9.0824,-4.1607,-2.7142,-3.2074,-1.514,0.6858,1.8223,3.3347,4.8416,7.3837,7.0652,9.2034,10.2454,9.5385,6.9355,5.7676,11.7166,12.7813,9.9436,6.7883,5.0857,1.3461,-1.7336,-4.5679,-8.9845,-10.9705,-10.3572,-9.4249,-8.2699,-10.8796,-12.5095,-11.2506,-9.2462,-4.1369,-2.569,-3.0828,-1.3894,0.9532,2.246,3.7537,5.1586,7.6984,7.4856,9.6487,10.5665,9.7582,7.2136,6.1464,12.0647,13.0251,10.2028,7.1898,5.5587,1.7344,-1.4293,-4.2163,-8.5756,-10.6768,-10.3009,-9.532,-8.4467,-11.2184,-13.1802,-12.2542,-10.4327,-5.4568,-4.1433,-4.9771,-3.4652,-1.1371,0.1223,1.4879,2.7969,5.4283,5.383,7.5994,8.4895,7.762,5.4292,4.5125,10.4211,11.3633,8.6645,5.8189,4.2182,0.3273,-2.7969,-5.3976,-9.6039,-11.6822,-11.263,-10.2773,-8.8996,-11.4955,-13.3762,-12.2751,-10.1528,-4.9423,-3.5751,-4.3983,-2.7671,-0.3038,0.9177,2.0976,3.2755,5.8851,5.7566,7.7285,8.365,7.5464,5.2106,4.1962,9.9346,10.8334,8.2704,5.5593,3.9706,0.0988,-2.8439,-5.1886,-9.2656,-11.3391,-10.8506,-9.6773,-8.1718,-10.8205,-12.8005,-11.674,-9.4695,-4.3073,-3.1079,-4.0175,-2.3219,0.1878,1.3142,2.385,3.6082,6.3462,6.2295,8.0894,8.6777,7.9391,5.6375,4.4786,10.0154,10.8301,8.2419,5.374,3.4966,-0.5954,-3.6053,-6.0218,-10.3017,-12.5843,-12.1301,-10.8699,-9.3521,-12.0788,-14.04,-12.7225,-10.3244,-5.1123,-3.9006,-4.663,-2.7406,-0.1244,0.9675,2.0536,3.4317,6.3099,6.1818,7.9939,8.6859,8.1644,6.0007,4.8775,10.5364,11.6491,9.3748,6.6827,4.9371,1.1068,-1.5606,-3.7615,-7.9938,-10.2182,-9.6034,-8.2344,-6.8104,-9.7322,-11.798,-10.5186,-8.2798,-3.3746,-2.4225,-3.2842,-1.4394,0.973,1.8323,2.8472,4.2886,7.1643,6.9197,8.6766,9.4753,9.0712,6.8703,5.6368,11.298,12.503,10.2141,7.3533,5.4679,1.6445,-1.0029,-3.3267,-7.737,-9.9992,-9.2929,-7.8985,-6.5711,-9.5379,-11.4842,-10.0623,-7.8307,-3.0139,-2.0458,-2.802,-0.9625,1.27,1.9482,2.9052,4.2853,6.934,6.3753,7.9335,8.6558,8.1118,5.6484,4.2073,9.8466,11.0917,8.7324,5.7532,3.8916,0.2319,-2.312,-4.672,-9.0969,-11.2255,-10.3629,-8.9659,-7.7302,-10.6849,-12.5017,-11.0199,-8.8696,-4.1043,-3.0161,-3.5923,-1.6873,0.5507,1.3692,2.6131,4.2298,6.9732,6.5191,8.3218,9.3072,8.8555,6.3539,4.9351,10.6767,11.9148,9.3535,6.1407,4.1671,0.4205,-2.3565,-5.0421,-9.6769,-11.8621,-11.0819,-9.8816,-8.8003,-11.7179,-13.4099,-11.9047,-9.8029,-4.9683,-3.6671,-4.0794,-2.1661,0.0694,1.024,2.4449,4.0938,6.7571,6.3118,8.2575,9.3422,8.847,6.3094,5.022,10.9728,12.3088,9.7554,6.6528,4.9311,1.39,-1.3458,-4.0205,-8.5219,-10.5421,-9.7595,-8.7033,-7.715,-10.62,-12.3664,-11.0904,-9.2464,-4.5257,-3.2651,-3.8282,-2.1569,-0.0535,0.9395,2.3945,3.9523,6.5292,6.1642,8.2769,9.4161,8.8628,6.3446,5.2137,11.2834,12.5765,9.949,6.915,5.3409,1.8385,-0.9614,-3.6031,-7.9006,-9.7128,-8.846,-7.7068,-6.4679,-9.024,-10.5271,-9.1295,-7.1039,-2.0781,-0.5663,-1.0747,0.5671,2.7283,3.8077,5.1835,6.489,8.8397,8.3643,10.3303,11.1607,10.2628,7.5536,6.3434,12.2563,13.3013,10.5266,7.531,6.0182,2.4523,-0.4332,-3.0089,-7.142,-8.8954,-8.1058,-6.9955,-5.6485,-8.1164,-9.6959,-8.4322,-6.4105,-1.2934,0.2122,-0.426,1.1517,3.4121,4.6013,5.9447,7.1705,9.5737,9.2427,11.24,11.9448,10.9422,8.2455,7.0131,12.7135,13.4512,10.4657,7.3344,5.5719,1.6099,-1.6256,-4.3879,-8.6787,-10.7103,-10.2239,-9.2483,-7.8871,-10.3891,-12.1019,-10.8957,-8.75,-3.4747,-1.945,-2.617,-0.9455,1.5168,2.8173,4.1274,5.354,7.9122,7.7586,9.7961,10.4862,9.6106,7.1759,6.146,11.9287,12.7996,10.1141,7.3106,5.7266,1.8552,-1.1939,-3.6877,-7.8312,-9.9088,-9.4943,-8.4916,-7.1292,-9.8222,-11.8445,-10.8615,-8.8387,-3.7674,-2.5816,-3.5572,-2.0082,0.3985,1.5413,2.6549,3.8411,6.5206,6.4523,8.455,9.1474,8.4381,6.2084,5.2379,10.984,11.9149,9.3991,6.6843,5.0281,1.0894,-1.8781,-4.2488,-8.4023,-10.5705,-10.1279,-8.9569,-7.4765,-10.1927,-12.2257,-11.103,-8.9044,-3.81,-2.7175,-3.7111,-2.0819,0.3134,1.2608,2.1523,3.2429,5.858,5.5809,7.2773,7.7729,7.0181,4.7167,3.5564,9.1495,10.1179,7.7325,5.0517,3.3592,-0.4953,-3.227,-5.4013,-9.5092,-11.6416,-11.0222,-9.628,-8.0671,-10.8187,-12.8031,-11.4986,-9.1609,-4.0731,-2.9849,-3.817,-1.9442,0.5876,1.5819,2.6235,4.0116,6.8852,6.7047,8.4646,9.1441,8.604,6.3498,5.0778,10.5945,11.579,9.13,6.1942,4.1984,0.1694,-2.6714,-5.0701,-9.4991,-11.8437,-11.2575,-9.885,-8.455,-11.3172,-13.2253,-11.7326,-9.2961,-4.2063,-3.0274,-3.6211,-1.5408,1.0333,2.0248,3.1883,4.7587,7.6813,7.4147,9.1508,9.947,9.502,7.1926,5.8385,11.4228,12.5661,10.169,7.1632,5.162,1.2649,-1.4773,-3.9546,-8.5294,-10.9135,-10.3153,-9.0759,-7.939,-11.0594,-13.0995,-11.7649,-9.6446,-4.9088,-3.9316,-4.6129,-2.6958,-0.3789,0.4532,1.6449,3.2722,6.1303,5.7775,7.5813,8.551,8.1805,5.8114,4.4451,10.157,11.4135,8.9668,5.8518,3.8715,0.0966,-2.6134,-5.1772,-9.7672,-11.9907,-11.2033,-9.9001,-8.7323,-11.6725,-13.4189,-11.8829,-9.7036,-4.8783,-3.6843,-4.1742,-2.2542,-0.0363,0.7829,2.0279,3.5826,6.203,5.637,7.3737,8.2923,7.7371,5.1433,3.7108,9.5038,10.795,8.2838,5.1803,3.398,-0.1367,-2.7365,-5.251,-9.6694,-11.6181,-10.6556,-9.3403,-8.1514,-10.9414,-12.5395,-11.0209,-8.9468,-4.1156,-2.8034,-3.2523,-1.4282,0.7318,1.658,3.0459,4.6064,7.1588,6.6397,8.5334,9.5189,8.8642,6.169,4.7601,10.577,11.7202,8.954,5.689,3.8535,0.1895,-2.6827,-5.4376,-9.9219,-11.8737,-11.0225,-9.8684,-8.6937,-11.3398,-12.8309,-11.3294,-9.241,-4.2327,-2.7014,-3.075,-1.2745,0.9565,2.0661,3.5612,5.0688,7.5718,7.156,9.2037,10.2191,9.5205,6.9093,5.7404,11.7659,12.9921,10.3342,7.3435,5.836,2.3605,-0.4348,-3.0373,-7.2711,-9.0678,-8.2596,-7.1972,-6.0115,-8.6384,-10.2823,-9.0649,-7.1847,-2.2606,-0.8628,-1.5116,0.0283,2.175,3.288,4.6882,6.0279,8.4839,8.1801,10.3103,11.2632,10.4931,7.9529,6.9041,12.9051,13.9803,11.2419,8.2922,6.772,3.1224,0.1373,-2.5024,-6.6969,-8.5643,-7.9189,-6.9138,-5.6256,-8.1698,-9.8731,-8.7283,-6.7759,-1.7188,-0.3272,-1.1196,0.3299,2.497,3.5668,4.7392,5.801,8.1039,7.7082,9.6226,10.2471,9.2391,6.6258,5.5039,11.3087,12.1927,9.4393,6.5832,5.0661,1.3196,-1.6755,-4.1725,-8.2458,-10.1499,-9.5817,-8.5212,-7.1038,-9.6383,-11.457,-10.3553,-8.2892,-3.1201,-1.7486,-2.5721,-0.9846,1.4367,2.6785,3.9205,5.1338,7.7448,7.6427,9.6851,10.3729,9.534,7.1396,6.0837,11.7769,12.5603,9.8092,6.9066,5.1576,1.1103,-2.0607,-4.639,-8.885,-11.0711,-10.6986,-9.6475,-8.2041,-10.8255,-12.7449,-11.5777,-9.3224,-4.0449,-2.6879,-3.4714,-1.6967,0.9034,2.1593,3.3451,4.6189,7.3794,7.3224,9.2705,9.913,9.1876,6.9237,5.8619,11.5048,12.3813,9.8403,7.0629,5.3025,1.2809,-1.7254,-4.1493,-8.418,-10.724,-10.3818,-9.2904,-7.9314,-10.8098,-12.9748,-11.9193,-9.7706,-4.7487,-3.7071,-4.6655,-2.93,-0.4239,0.6289,1.6789,3.0024,5.8613,5.7799,7.6488,8.3442,7.7916,5.6201,4.5034,10.1146,11.1071,8.7027,5.9166,4.0758,0.1024,-2.7206,-5.0141,-9.2694,-11.5059,-10.9079,-9.4999,-7.9401,-10.6771,-12.5732,-11.1251,-8.6641,-3.496,-2.3182,-3.0261,-1.0524,1.4991,2.4599,3.478,4.8462,7.642,7.3186,8.9405,9.5334,8.9196,6.5603,5.1893,10.6832,11.7179,9.3268,6.4382,4.5357,0.6779,-1.9681,-4.2121,-8.5072,-10.6828,-9.9047,-8.3898,-6.8896,-9.6965,-11.525,-9.9729,-7.5502,-2.5186,-1.3731,-1.9746,0.0607,2.5342,3.4294,4.5421,6.0616,8.8741,8.4591,10.071,10.7639,10.169,7.6402,6.0563,11.4467,12.3946,9.7498,6.4745,4.2576,0.2097,-2.6739,-5.2988,-9.9637,-12.3311,-11.6483,-10.3165,-9.0633,-11.9843,-13.748,-12.1365,-9.7774,-4.7985,-3.5404,-3.9533,-1.8496,0.5838,1.5246,2.8224,4.4909,7.3024,6.8767,8.6377,9.5665,9.1057,6.6215,5.1889,10.8957,12.1469,9.6647,6.535,4.6052,0.9037,-1.7785,-4.3534,-8.9337,-11.1232\",\"fieldId\":\"dyna_value\"},{\"fieldValue\":3.82460356,\"fieldId\":\"v2x\"},{\"fieldValue\":215.790222,\"fieldId\":\"p1x\"},{\"fieldValue\":2.8295362,\"fieldId\":\"vsx2\"},{\"fieldValue\":1.96432722,\"fieldId\":\"vsx1\"},{\"fieldValue\":193.211624,\"fieldId\":\"p2x\"},{\"fieldValue\":20.5193768,\"fieldId\":\"v1x\"},{\"fieldValue\":\"\",\"fieldId\":\"reserve\"},{\"fieldValue\":0.8084384,\"fieldId\":\"v3x\"},{\"fieldValue\":4.711441,\"fieldId\":\"rv\"},{\"fieldValue\":156,\"fieldId\":\"lmjc\"},{\"fieldValue\":0.688949943,\"fieldId\":\"average\"},{\"fieldValue\":-7.285991,\"fieldId\":\"gap\"}]}";

    private static String KEY_TABLENAME = "{tableName}";
    private static String KEY_TIMESTAMP = "{timestamp}";
    private static String KEY_FACTORYID = "{factory_id}";
    private static String KEY_EQUIPMENTID = "{equipment_id}";
    private static String KEY_CHANNELID = "{channel_id}";
    private static String KEY_MACHINEID = "{machine_id}";

    private static Map<String, List<String>> idMap = new HashMap<String, List<String>>();

    private static int index = 0;
    private static List<String> idList = new ArrayList<String>();

    public static String build() {
        // print idmap

//        for(Map.Entry<String, List<String>> kv : idMap.entrySet()){
//            System.out.println(kv.getKey() + "\t" + kv.getValue().size());
//        }

        if(index >= idList.size()){
            index = 0;
        }

        String key = idList.get(index);

        index++;

        List<JSONObject> objs = new ArrayList<JSONObject>();
        for(String idVal : idMap.get(key)){
            String[] ids = idVal.split("_");
            objs.add(JSON.parseObject(S8kDataTempJson
                    .replace(KEY_FACTORYID, ids[0])
                    .replace(KEY_MACHINEID, ids[1])
                    .replace(KEY_EQUIPMENTID, ids[2])
                    .replace(KEY_TABLENAME, "c_vib")
                    .replace(KEY_TIMESTAMP, TimeUtils.now() + "")
                    .replace(KEY_CHANNELID, "1")));
        }
        return JSON.toJSONString(objs);
    }

    public static void main(String[] args){
        String msg = S8kDataBuilder.build();
        System.out.println(msg);
    }

    private static String[] ids = {
            "5_0_0",
            "5_0_1",
            "5_0_2",
            "6_0_0",
            "7_0_0",
            "8_0_0",
            "8_0_1",
            "222_0_1",
            "222_0_2",
            "222_0_3",
            "222_0_4",
            "11_0_0",
            "11_0_1",
            "11_0_2",
            "11_0_3",
            "50_0_0",
            "50_0_1",
            "50_0_2",
            "50_0_3",
            "50_0_4",
            "60_0_0",
            "60_0_1",
            "60_0_2",
            "60_0_3",
            "63_0_0",
            "63_0_1",
            "102_0_0",
            "103_0_0",
            "103_0_1",
            "103_1_2",
            "103_1_4",
            "104_0_0",
            "105_0_0",
            "106_0_0",
            "107_0_0",
            "107_0_1",
            "111_0_0",
            "111_0_1",
            "111_0_2",
            "132_0_0",
            "133_0_0",
            "136_0_0",
            "136_2_8",
            "136_0_1",
            "136_1_2",
            "136_1_3",
            "136_1_4",
            "136_1_5",
            "136_2_6",
            "136_2_7",
            "140_0_0",
            "141_0_0",
            "141_0_1",
            "146_0_0",
            "146_0_1",
            "150_0_0",
            "150_0_1",
            "150_0_2",
            "154_0_0",
            "155_0_0",
            "157_0_0",
            "157_0_2",
            "172_0_0",
            "177_0_0",
            "177_0_1",
            "177_0_2",
            "177_0_3",
            "177_0_4",
            "179_0_0",
            "179_0_1",
            "183_0_0",
            "185_0_0",
            "185_0_1",
            "189_0_0",
            "190_0_0",
            "190_0_1",
            "190_0_2",
            "190_0_3",
            "195_0_0",
            "197_0_0",
            "197_0_1",
            "197_0_2",
            "197_0_3",
            "197_0_4",
            "197_3_8",
            "197_3_9",
            "198_0_0",
            "198_0_1",
            "198_1_3",
            "199_0_0",
            "199_0_1",
            "199_1_4",
            "199_1_5",
            "200_0_0",
            "201_0_0",
            "204_0_0",
            "204_0_1",
            "204_0_2",
            "205_0_0",
            "205_0_1",
            "210_0_0",
            "210_0_1",
            "211_0_0",
            "213_0_0",
            "213_0_1",
            "213_0_2",
            "215_0_0",
            "215_0_1",
            "216_0_0",
            "216_0_1",
            "220_0_0",
            "222_0_0",
            "222_1_2",
            "222_1_3",
            "222_1_4",
            "225_0_0",
            "226_0_0",
            "226_0_1",
            "227_0_0",
            "227_0_1",
            "228_0_0",
            "230_0_0",
            "230_0_1",
            "235_0_0",
            "245_0_1",
            "245_0_2",
            "245_0_3",
            "245_0_4",
            "245_0_5",
            "246_0_0",
            "247_0_0",
            "247_0_1",
            "251_0_0",
            "251_0_1",
            "251_0_2",
            "271_0_1",
            "271_0_2",
            "280_0_0",
            "280_0_1",
            "280_1_3",
            "289_0_0",
            "292_0_0",
            "292_0_1",
            "328_0_0",
            "328_1_1",
            "338_0_0",
            "338_0_1",
            "338_1_2",
            "338_2_3",
            "338_1_4",
            "338_1_5",
            "338_1_6",
            "338_1_7",
            "338_1_8",
            "338_0_9",
            "338_0_10",
            "338_3_12",
            "338_3_13",
            "338_3_14",
            "344_0_0",
            "344_0_1",
            "355_0_0",
            "356_0_0",
            "358_0_0",
            "358_1_1",
            "364_0_0",
            "371_0_0",
            "371_0_1",
            "371_0_2",
            "372_0_0",
            "375_0_0",
            "375_0_1",
            "378_0_0",
            "379_1_2",
            "379_1_3",
            "379_2_4",
            "379_2_5",
            "380_0_0",
            "381_0_0",
            "387_0_0",
            "387_0_1",
            "389_0_0",
            "404_0_0",
            "404_0_1",
            "411_0_0",
            "411_0_1",
            "412_0_0",
            "416_0_0",
            "418_0_0",
            "418_0_1",
            "432_0_0",
            "441_0_0",
            "441_1_1",
            "441_0_2",
            "441_1_3",
            "455_0_0",
            "455_0_1",
            "455_0_2",
            "459_0_0",
            "467_0_0",
            "467_0_1",
            "472_0_0",
            "475_0_0",
            "478_0_0",
            "478_0_1",
            "484_0_0",
            "486_0_0",
            "497_0_0",
            "502_0_0",
            "504_0_0",
            "527_0_0",
            "546_0_0",
            "549_0_0",
            "549_0_1",
            "549_0_2",
            "549_0_3",
            "549_0_4",
            "549_1_5",
            "549_1_6",
            "549_1_7",
            "556_0_0",
            "556_0_1",
            "556_0_2",
            "566_0_0",
            "573_0_0",
            "580_0_0",
            "585_0_0",
            "591_0_0",
            "591_0_1",
            "592_0_0",
            "592_0_1",
            "598_0_0",
            "608_0_0",
            "608_0_1",
            "608_0_2",
            "608_0_3",
            "618_0_0",
            "618_0_1",
            "624_0_0",
            "624_0_1",
            "630_0_0",
            "630_0_1",
            "632_0_0",
            "647_0_0",
            "647_0_1",
            "651_0_0",
            "659_0_0",
            "673_0_0",
            "695_0_0",
            "698_0_0",
            "699_0_0",
            "701_0_0",
            "704_0_0",
            "704_0_1",
            "706_0_0",
            "708_0_0",
            "708_0_1",
            "713_0_0",
            "729_0_0",
            "729_0_1",
            "735_0_0",
            "755_0_0",
            "765_0_0",
            "765_0_1",
            "769_0_0",
            "769_0_1",
            "773_0_0",
            "780_0_0",
            "784_0_0",
            "787_0_0",
            "789_0_0",
            "794_0_0",
            "794_0_1",
            "794_1_2",
            "794_1_3",
            "795_0_0",
            "801_0_0",
            "805_0_0",
            "810_0_0",
            "816_0_0",
            "816_0_1",
            "816_0_2",
            "846_0_0",
            "847_0_0",
            "865_0_0",
            "875_0_0",
            "894_0_0",
            "899_0_0",
            "900_0_0",
            "907_0_0",
            "919_0_0",
            "919_0_1",
            "925_0_0",
            "930_0_0",
            "951_0_0",
            "951_0_1",
            "951_0_2",
            "965_0_0",
            "986_0_0",
            "991_0_0",
            "992_0_0",
            "992_0_1",
            "993_0_0"
    };

    static {
        for(String id : ids){
            String[] ss = id.split("_");
            List<String> ss0 = idMap.get(ss[0]);
            if(ss0 == null){
                ss0 = new ArrayList<String>();
                idMap.put(ss[0], ss0);
            }
            ss0.add(id);
            idList.add(ss[0]);
        }
    }
}
